package com.douqu.game.auth.server;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author wangzhenfei
 *         2018-04-02 12:22
 */
public class SmsCodeCache {
    private static SmsCodeCache cache;

    private Map<String,SmsInfo> cacheMap = new ConcurrentHashMap<>();

    private SmsCodeCache(){}

    public static SmsCodeCache getInstance(){
        if(cache == null){
            cache = new SmsCodeCache();
        }
        return cache;
    }

    public void addCache(String phone, String code){
        cacheMap.put(phone, new SmsInfo(code, System.currentTimeMillis() + 60000));
    }

    public Map<String, SmsInfo> getCacheMap() {
        return cacheMap;
    }

    public boolean checkCode(String phone, String code){
        SmsInfo info = cacheMap.get(phone);
        return info == null ? false : info.code.equals(code);
    }

    public static class SmsInfo{
        public String code = "";
        public long endTime;

        public SmsInfo(String code, long endTime) {
            this.code = code;
            this.endTime = endTime;
        }

        public boolean isTimeOut(){
            return System.currentTimeMillis() > endTime;
        }
    }
}
